# This is a test

import os, unittest

import aphw

class Tests(unittest.TestCase):

    def cleanup(self):
        try:
            os.remove(self.db_name)
        except OSError:
            pass

    def setUp(self):
        self.db_name = 'test.db'
        self.cleanup()

        self.db = aphw.Database(self.db_name)

    def tearDown(self):
        self.db.close()
        self.cleanup()

    def test_create(self):
        with aphw.Cursor(self.db) as c:
            pass

    def test_move_first(self):
        with aphw.Cursor(self.db) as c:
            self.assertRaises(
                aphw.StatusError,
                c.move_first)

        for i in range(1000):
            self.db.insert(i, str(i))
            
        with aphw.Cursor(self.db) as c:
            c.move_first()
            self.assert_(c.key.value == 0)

            for row in c:
                self.db.erase(c.key)

            self.assertRaises(
                aphw.StatusError,
                c.move_first)

    def test_move_last(self):
        with aphw.Cursor(self.db) as c:
            self.assertRaises(
                aphw.StatusError,
                c.move_last)

        for i in range(1000):
            self.db.insert(i, str(i))
            
        with aphw.Cursor(self.db) as c:
            c.move_last()

            for i in range(999):
                c.move_previous()
            self.assertRaises(
                aphw.StatusError,
                c.move_previous)

            for row in c:
                self.db.erase(c.key)

            self.assertRaises(
                aphw.StatusError,
                c.move_last)

    def test_move_next(self):
        for i in range(1000):
            self.db.insert(i, str(i))

        with aphw.Cursor(self.db) as c:
            expected = [r.key.value for r in c]
            self.assert_(len(expected) == 1000)

            c.move_first()
            
            found = set([c.key.value])
            for i in range(999):
                c.move_next()
                found.add(c.key.value)
            
            self.assertRaises(
                aphw.StatusError,
                c.move_next)

            found = list(found)

            found.sort()
            expected.sort()

            self.assert_(found == expected)

    def test_move_previous(self):
        for i in range(1000):
            self.db.insert(i, str(i))

        with aphw.Cursor(self.db) as c:
            expected = [r.key.value for r in c]
            self.assert_(len(expected) == 1000)

            c.move_last()
            
            found = set([c.key.value])
            for i in range(999):
                c.move_previous()
                found.add(c.key.value)
            
            self.assertRaises(
                aphw.StatusError,
                c.move_previous)

            found = list(found)

            found.sort()
            expected.sort()

            self.assert_(found == expected)

    def test_record(self):
        for i in range(1000):
            self.db.insert(i, str(i))

        with aphw.Cursor(self.db) as c:
            c.move_first()

            self.assert_(str(c.key.value) == str(c.record))
            
            for i in range(999):
                c.move_next()
                self.assert_(str(c.key.value) == str(c.record))

    def test_iteration(self):
        for i in range(1000):
            self.db.insert(i, str(i))

        with aphw.Cursor(self.db) as c:
            keys = [r.key.value for r in c]
            for r in c:
                self.assert_(str(r.key.value) == str(r.record))

def suite():
    cases = [Tests,
             ]
    suites = [unittest.TestLoader().loadTestsFromTestCase(c) for c in cases]
    return unittest.TestSuite(suites)

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())
